<?php
namespace app\api\controller;
use app\BaseController;
use think\facade\Db;
use think\facade\Cache;
use think\Response;
header('Access-Control-Allow-Origin: *');
header("Access-Control-Allow-Headers: x-token, x-uid, Origin, X-Requested-With, Content-Type, Accept");
header('Access-Control-Allow-Methods: GET, POST, PUT, OPATION');
header('content-type:application/json;charset=utf8');
class Base extends BaseController
{
    /*定义一个初始数组*/
    protected $info = array(
        'code'=>200,//200 正常 10001 验证失败  10002未登录   10003 账号不存在
        'msg'=>'提示',
        'total'=>0,//数据数量
        'data'=>[]
    );

    /*获取登录的管理员信息*/
    protected function userInfo(){
        $token= request()->header('token');
        return checkToken($token)['data'];
    }

    //写入操作日志
    protected function logData($action='',$admin_id=''){
        $pt['time'] = time();
        if($admin_id){
            $pt['admin_id'] =  $admin_id;
        }else{
            $pt['admin_id'] = $this->userInfo()['uid'];
        }
        $pt['action'] =  trim($action);
        Db::table('an_admin_log')->insert($pt);
    }

    /**
     * 限制某ip一定时间内请求次数(防恶意请求接口)
     * 正常请求返回true,超过请求次数的时间返回false
     * @param int $num 超过3次不让访问(这里没写错,就是超过3次,不是2次)
     * @param int $time 禁止访问后,距离允许下次请求的间隔时间(单位：秒,这里默认是3秒内不允许再次请求)
     * @return bool
     */
    protected function getResult($num = 2, $time = 3){
        $ip = request()->ip();    //获取来源ip
        $path = request()->url(); //获取当前地址
        $url = $ip . $path;       //拿ip和地址拼接成key
        // Cache::delete($url); 
        $res = Cache::get($url);  //获取value
        if ($res) {               //如果有value(说明之前已经请求过了)
            if ($res > $num) {    //超过规定请求的次数
                return false;
            }
            Cache::inc($url, 1);    //没超过规定请求的次数,次数自增1
            return true;
        } else {
            Cache::set($url, 1, $time); //初次请求,写入缓存与有效时间
            return true;
        }
    }

    protected function initialize(){
        //获取当前控制器+方法
        $api=request()->controller().'/'.request()->action();

        //过滤不需要验证的接口
        if (in_array($api,config('an_config.noToken'))){
            return true;
        }

        $client= request()->header('client');//获取客户端名字

        //从header里获取token
        $token= request()->header('token');

        //检查token
        $result = checkToken($token);

        //token校验结果判断拦截
        if($result['status'] != 200){
            $this->info['code'] = $result['status'];
            $this->info['msg'] = $result['info'];
            Response::create($this->info, 'json')->send();
            exit;
        }else{

            //校验token中的id 是否 跟session中id一致
            $userData = session('adminData');
            if(empty($userData)){
                $this->info['code'] = 401;
                $this->info['msg'] = '登录状态过期，请重新登录';
                $this->info['data'] = $userData;
                Response::create($this->info, 'json')->send();
                exit;
            }else{
                if($this->userInfo()['uid'] != $userData['id']){
                    $this->info['code'] = 401;
                    $this->info['msg'] = '账号异常，请重新登录';
                    $this->info['data'] = $userData;
                    Response::create($this->info, 'json')->send();
                    exit;
                }
            }

            //权限校验
            // $adminNode = session('adminNode');
        }
     
    }
}
